﻿package com.saint.video
{
	import caurina.transitions.Tweener;
	import com.saint.utils.Logger;
	import com.saint.video.data.VideoDataObject;
	import com.saint.video.events.VideoControlsEvent;
	import com.saint.video.events.VideoEvent;
	import com.saint.video.SimpleVideo;
	import com.saint.video.videoControls.VideoControls;
	import com.saint.video.videoControls.VideoControls;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.ui.Mouse;
	import flash.utils.getTimer;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author ...
	 */
	public class VideoController extends EventDispatcher
	{
		
		private var video:SimpleVideo;
		private var videoControls:VideoControls;
		private var paused:Boolean = false;
		private var watch:Timer;
		private var videoStarted:Boolean = false;
		
		public var videoToPlay:String;
		
		/* variables used hiding and showing the controls */
		private var controlsVisible:Boolean = true;
		private var lastMouseMoveTime:Number = 0;
		private var controlsHideDelay:Number = 2000;
		private var hideControls:Boolean = false;
		private var playerContainer:Sprite;
		
		public function VideoController(vidPlayer:SimpleVideo, vidControls:VideoControls, container:Sprite)
		{
			video= vidPlayer;
			videoControls = vidControls;
			playerContainer = container;
			
			// set up the listeners
			setUpControlListeners(videoControls);
			setUpVideoListeners(video) ;
			
		}
		
		public function init(autoPlay:Boolean = true):void
		{
			trace("videoController.init hideContols: " + hideControls);
			
			//start  watching for mouse movement to show and hide the video
			playerContainer.addEventListener(Event.ENTER_FRAME, watchMouse, false, 0 , true);
			
			//create button
			var mouseDetectArea:Sprite = new Sprite();
			
			//  if mouse is over the 'screen' check for mouse movement
			playerContainer.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
						
			//start the video watching it's played durations
			startWatching();
			
			//start the video playing if autoplay is true
			if (autoPlay) startVideo();			
		}
		
		
		/* startVideo
		 * 
		 * Tells the SimpleVideo player to initialte playback
		 * 
		 * */
		public function startVideo()
		{
			if (!videoStarted && videoToPlay != null)
			{
				video.playVideo(videoToPlay);
				videoStarted = true;
			}
		}
		
		
		/*
		 * PlayNewVideo 
		 * 
		 * resets the video, removes listeners and starts playback
		 * 
		 * */
		public function playNewVideo(flvFile:String):void 
		{
			videoStarted = false;
			videoToPlay = flvFile;
			video.reset();
			startVideo();
		}
		
		
		
		/** LISTENERS **/
		
		
		/**
		 * set up the listeners of the video controls (videoControls);
		 * @param	vidcontrols
		 */
		private function setUpControlListeners(vidControls:VideoControls):void
		{
			//play
			vidControls.addEventListener(VideoControlsEvent.PLAY, onControlsPlayPress);
			//pause
			vidControls.addEventListener(VideoControlsEvent.PAUSE, onControlsPausePress);
			//scrubber
			vidControls.addEventListener(VideoControlsEvent.SCRUBSTART, onControlsScrubStart);
			vidControls.addEventListener(VideoControlsEvent.SCRUBSTOP, onControlsScrubStop);
			vidControls.addEventListener(VideoControlsEvent.SCRUBBING, onControlsScrub);
			//volume
			vidControls.addEventListener(VideoControlsEvent.VOLUME_CHANGE, onVolumeChange)
		}
	
		

	
		
		/**
		 * set up the listeners on video (simpleVideo);
		 * @param	vid
		 */
		private function setUpVideoListeners(vid:SimpleVideo):void
		{
			vid.addEventListener(VideoEvent.META_DATA_DURATION, videoEventHandler, false, 0, true);
			vid.addEventListener(VideoEvent.STREAMING_PECENTAGE, videoStreamUpdateHandler, false, 0, true);
			vid.addEventListener(VideoEvent.PLAY_STOPPED, endOfStreamHandler, false, 0, true);
		}

		
		
		
		
		/**
		 * starts and stops watching the video time
		 */
		private function startWatching():void {
			watch = new Timer(33);
            watch.addEventListener("timer", timerHandler, false, 0, true);
            watch.start();
		}
		
	//TODO - FINISH INVESTIGATING THE DELAY BETWEEN ROLLOVER AN BRINGIN IN CONTROLS
	
	// PROBABLY JUST A TWEEN ISSUE
		
		/**		 *  CONTOL HIDING - REVEALING	THE CONTROLS*/
		
		private function watchMouse(e:Event):void
		{
			
			//check when last mouse movement was
			((getTimer() - lastMouseMoveTime) > controlsHideDelay )  ? hideControls = true : hideControls = false;
			
			//show or hide controls
			if (hideControls)
			{
				if (controlsVisible)  tweenOutControls()  ;
			}else {
				if (!controlsVisible)  tweenInControls();
			}
		}
		
		
		private function onMouseMove(e:MouseEvent):void
		{
			lastMouseMoveTime = getTimer();
		}
		
		private function tweenInControls():void
		{
			controlsVisible = true;
			Tweener.addTween(videoControls, { alpha: 1, time:1 , transition: "easeOutQuint",  onComplete: onControlTweenInComplete} );
		}
		
		private function tweenOutControls():void
		{
			controlsVisible = false;
			Tweener.addTween(videoControls, { alpha: 0, time:1, transition: "easeOutQuint", onComplete:onControlsTweenOutcomplete } );
		}
		
		private function onControlsTweenOutcomplete():void
		{
		}
		
		private function onControlTweenInComplete():void
		{
		}
		
		
		
		/** HANDLERS **/
		
		private function onControlsPlayPress(e:VideoControlsEvent):void
		{
			Logger.info("VideoController.onControlsPlayPress" )
			paused = false;
			if (videoStarted)
			{
				video.resumeVideo();
			}else {
				startVideo();
			}
		}
		
		private function onControlsPausePress(e:VideoControlsEvent):void
		{
			paused = true;
			video.pauseVideo();
		}
		private function onControlsScrubStart(e:VideoControlsEvent):void
		{
			video.pauseVideo();
			watch.stop()
		}
				
		private function onControlsScrubStop(e:VideoControlsEvent):void
		{
			watch.start();
			if (!paused) video.resumeVideo();
			video.seek(e.pos);
		}
		
		private function onControlsScrub(e:VideoControlsEvent):void
		{
				video.seek(e.pos);
		}
		
		private function onControlsSeek(e:VideoControlsEvent):void
		{
			video.seek(e.pos);
		}
		
		
				
		private function videoEventHandler(e:VideoEvent):void
		{
			videoControls.videoEventHandler(e.data);
		}
		
		private function videoStreamUpdateHandler(e:VideoEvent):void
		{
			videoControls.videoStreamUpdateHandler(e.data);
		}
		
		private function endOfStreamHandler(e:VideoEvent):void
		{
			videoControls.endOfStreamHandler(e.data);
			videoStarted = false;
		}
		
		private function timerHandler(e:TimerEvent):void
		{
			videoControls.timerHandler(video.currentTime);
		}
			
		private function onVolumeChange(e:VideoControlsEvent):void
		{
			trace("VideoControls.onVolumeChange " + e.pos);
			video.adjustVolume(e.pos);
		}
		
	
	}
	
}